package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> getUsers(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        List<User> users = userMapper.getUsers(userDto);
        Page<User> pageInfo = (Page<User>) users;
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void addUser(User user) {
        user.setPassword(user.getUsername() + "123");
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.addUser(user);

    }

    @Override
    public void deleteUsers(List<Integer> ids) {
        userMapper.deleteUsers(ids);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public void updateUser(User user) {
        user.setPassword(user.getUsername() + "123");
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public List<User> listUsers() {
        return userMapper.listUsers();
    }

    @Override
    public List<User> listUsersByRoleLabel(String roleLabel) {
        return userMapper.listUsersByRoleLabel(roleLabel);
    }

    @Override
    public List<User> listUsersByDeptId(Integer deptId) {
        return userMapper.listUsersByDeptId(deptId);
    }

    @Override
    public LoginResultVo login(User user) {
        //调用mapper接口获取用户信息
        User u = userMapper.getUserByUsername(user.getUsername());
        if (u == null) {
           throw new BusinessException("用户不存在");
        }
        //密码校验
        if (!u.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))) {
            throw new BusinessException("密码错误");
        }
        //校验状态
        if (u.getStatus() == 0) {
            throw new BusinessException("用户被禁用");
        }
        //返回结果
        LoginResultVo loginResult = new LoginResultVo();
        loginResult.setId(u.getId());
        loginResult.setUsername(u.getUsername());
        loginResult.setName(u.getName());
        loginResult.setImage(u.getImage());
        loginResult.setRoleLabel(u.getRoleLabel());
        //生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", u.getId());
        claims.put("username", u.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        loginResult.setToken(jwt);

        return loginResult;
    }
}
